﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using TGB.Core.Infrastructure;
using TGBTech.Core.Models.DanhMuc;
using TGBTech.Core.Models.Models.ChungTu;

namespace TGBTech.WebAdmin.Controllers
{
    public class ChungTuXuatsController : Controller
    {
        private TGBTechDb db = new TGBTechDb();

        // GET: ChungTuXuats
        public ActionResult Index()
        {
            // Data for table ChungTus
            var chungTuXuats = db.ChungTuXuats.Include(c => c.Khachhang).Include(c => c.TaiKhoanCo).Include(c => c.TaiKhoanNo);
            ViewBag.ListChungTu = chungTuXuats.ToList();

            // Data for form Insert
            ViewBag.KhachHangId = new SelectList(db.KhachHang, "Id", "Name");
            ViewBag.TaiKhoanCoId = new SelectList(db.TaiKhoan, "Id", "Name");
            ViewBag.TaiKhoanNoId = new SelectList(db.TaiKhoan, "Id", "Name");

            return View();
        }

        // GET: ChungTuXuats/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ChungTuXuat chungTuXuat = db.ChungTuXuats.Find(id);
            ViewBag.CTChungTu = db.CTChungTuXuats.Include(c => c.CTHangHoa).Include(c => c.CTHangHoa.HangHoa).Where(p => p.ChungTuXuatId == id).ToList();
            if (chungTuXuat == null)
            {
                return HttpNotFound();
            }

            // Get Chi Tiet hang Hoa
            var cthh = db.ChiTietHangHoa.Include(c => c.HangHoa).Where(p => p.SoLuongTon > 0)
                .Select(p=> new
                {
                    Id = p.Id,
                    Display = p.HangHoa.Id + " | " + p.HangHoa.Name + " | Tồn " + p.SoLuongTon 
                }).ToList();
            
            ViewBag.CTHangHoaId = new SelectList(cthh, "Id", "Display");

            ViewBag.ChungTuXuat = chungTuXuat;
            return View();
        }


        // GET: ChungTuXuats/Create
        public ActionResult Create()
        {
            ViewBag.KhachHangId = new SelectList(db.KhachHang, "Id", "Name");
            ViewBag.TaiKhoanCoId = new SelectList(db.TaiKhoan, "Id", "Name");
            ViewBag.TaiKhoanNoId = new SelectList(db.TaiKhoan, "Id", "Name");
            return View();
        }

        // POST: ChungTuXuats/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "Id,SoCT,NgayCT,TaiKhoanNoId,TaiKhoanCoId,KhachHangId,LyDo")] ChungTuXuat chungTuXuat)
        {
            if (ModelState.IsValid)
            {
                db.ChungTuXuats.Add(chungTuXuat);
                db.SaveChanges();
                return Json(new
                {
                    SoCT = chungTuXuat.SoCT,
                    NgayCT = chungTuXuat.NgayCT.ToString("dd/MM/yyyy"),
                    No = chungTuXuat.TaiKhoanNoId,
                    Co = chungTuXuat.TaiKhoanCoId,
                    LyDo = chungTuXuat.LyDo,
                    KhachHang = chungTuXuat.KhachHangId,
                });
            }

            return Json(new { Error = "Error" });
        }

        // GET: ChungTuXuats/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ChungTuXuat chungTuXuat = db.ChungTuXuats.Find(id);
            if (chungTuXuat == null)
            {
                return HttpNotFound();
            }
            ViewBag.KhachHangId = new SelectList(db.KhachHang, "Id", "Name", chungTuXuat.KhachHangId);
            ViewBag.TaiKhoanCoId = new SelectList(db.TaiKhoan, "Id", "Name", chungTuXuat.TaiKhoanCoId);
            ViewBag.TaiKhoanNoId = new SelectList(db.TaiKhoan, "Id", "Name", chungTuXuat.TaiKhoanNoId);
            return View(chungTuXuat);
        }

        // POST: ChungTuXuats/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "Id,SoCT,NgayCT,TaiKhoanNoId,TaiKhoanCoId,KhachHangId,LyDo")] ChungTuXuat chungTuXuat)
        {
            if (ModelState.IsValid)
            {
                db.Entry(chungTuXuat).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.KhachHangId = new SelectList(db.KhachHang, "Id", "Name", chungTuXuat.KhachHangId);
            ViewBag.TaiKhoanCoId = new SelectList(db.TaiKhoan, "Id", "Name", chungTuXuat.TaiKhoanCoId);
            ViewBag.TaiKhoanNoId = new SelectList(db.TaiKhoan, "Id", "Name", chungTuXuat.TaiKhoanNoId);
            return View(chungTuXuat);
        }

        // GET: ChungTuXuats/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ChungTuXuat chungTuXuat = db.ChungTuXuats.Find(id);
            if (chungTuXuat == null)
            {
                return HttpNotFound();
            }
            return View(chungTuXuat);
        }

        // POST: ChungTuXuats/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            ChungTuXuat chungTuXuat = db.ChungTuXuats.Find(id);
            db.ChungTuXuats.Remove(chungTuXuat);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult CreateCTCT([Bind(Include = "Id,ChungTuXuatId,SoHieu,SoHoaDon,NgayHoaDon,CTHangHoaId,SoLuong,DonGia,VAT")] CTChungTuXuat cTChungTuXuat, int ChungTuId)
        {
            cTChungTuXuat.ChungTuXuatId = ChungTuId;
            cTChungTuXuat.NgayHoaDon = null;
            if (ModelState.IsValid)
            {
                ChiTietHangHoa cthh = db.ChiTietHangHoa.Find(cTChungTuXuat.CTHangHoaId);
                if(cthh.SoLuongTon >= cTChungTuXuat.SoLuong)
                {
                    db.CTChungTuXuats.Add(cTChungTuXuat);
                    db.SaveChanges();

                    cthh.SoLuongTon -= cTChungTuXuat.SoLuong;

                    db.Entry(cthh).State = EntityState.Modified;
                    db.SaveChanges();
                }


                
            }

            return RedirectToAction("Details", new { id = cTChungTuXuat.ChungTuXuatId });
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
